<template>
	<!--pages/login/login.wxml-->
	<view>
		<view class="login">
			<button style="width:524rpx" @click="wxLogin" open-type="getUserInfo" getuserinfo="getUserInfo"><text class="text">微信登录</text></button>
			<button style="width:524rpx" class="register" @click="goLogin"><text class="text">手机号登录/注册</text></button>
		</view>
		<view class="protocol">
			<text style="color:#B5B5B5">登录或注册表示同意</text>
			<text>《用户协议》</text>
			<text style="color:#B5B5B5">与</text>
			<text>《隐私权证策》</text>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {};
	},
	methods: {
		wxLogin: function() {
			// uni-app登录api
			wx.login({
				success(res) {
					console.log('loginres>>>>>', res);
					let code = res.code; //发送给服务器的code字段,做登录判断
					console.log('code>>>>', code);
					// uni-app获取用户信息的api
					uni.getUserInfo({
						success(res) {
							console.log('用户信息res》》》》', res);
							console.log("code的值》》》》",code);
							if (code) {
								uni.request({
									url: 'http://192.168.1.18:8086/admin/wx',
									data: {
										code: code
									},
									method: 'POST',
									header: {
										'content-type': 'application/x-www-form-urlencoded'
									},
									// 微信登录成功的回调
									success(res) {
										console.log(res.data);
										uni.showToast({
											title: '登录成功',
											icon: 'noce',
											duration: 2000
										});
									},
									fail(err) {
										console.log('err>>>', err);
									}
								});
							}
						},
						fail: (error) => {
							console.log('shibai')
						}
					});
				},
				fail(err) {
					console.log('login failed ' + err);
				}
			});
		},

		// 去登陆
		goLogin: function() {
			uni.navigateTo({
				url: '/pages/register/register'
			});
		}
	}
};
</script>

<style>
@import './login.css';
</style>
